package com.CGFrame.struts.bean.UserLogin;

import java.util.*;
import java.sql.*;
import com.CGFrame.struts.bean.*;
import com.CGFrame.struts.POJOs.*;

import org.apache.struts.util.*;

public class LoginBean {
	
	Reguser r=null;
	ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
	
	public LoginBean(Reguser r)
	{
		this.r=r;
	}
	public LoginBean(String regUserId)
	{
		this.r = (Reguser)pop.UniqueQuery("Reguser", "regUserId,"+regUserId);
	}

	public Company QueryCom()
	{		
		return r.getEmployee().getSector().getCompany();
	}
	
	public Set getProjects()
	{
		Company company = this.QueryCom();
		Iterator ite_ReguserRoles = r.getReguserroles().iterator();
		LinkedHashSet<Project> linkHS = new LinkedHashSet<Project>();
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		//查询用户的所有角色,如果有一个角色有查看所有项目的权限,则把用户所在公司的所有项目返回
		if(this.canViewAllPrj())
		{
			return company.getProjects();
		}
		
		Iterator ite_Projects = company.getProjects().iterator();
		while (ite_Projects.hasNext())
		{
			Project project = (Project)ite_Projects.next();
			List flag = pop.Query("Projectreguserrole", "project.projectId,"+project.getProjectId()+",reguserrole.reguser.regUserId,"+r.getRegUserId(), null, null);
			
			if (flag.size() != 0)
			{
				linkHS.add(project);
			}
		}
		
		
		return linkHS;
	}
	
	public LinkedList getProjectIds()
	{
		LinkedList<String[]> lnlist=new LinkedList<String[]>();
		for(Iterator iter=this.getProjects().iterator();iter.hasNext();){
			Project pj=(Project)iter.next();
			String pro[]={String.valueOf(pj.getProjectId()),pj.getProjectName()};
			lnlist.add(pro);
		}
		return lnlist;
	}
	
	
	public Vector QueryRoles()
	{
		Iterator it = r.getReguserroles().iterator();
		Vector v = new Vector();
		while(it.hasNext())
		{
			Reguserrole regrole = (Reguserrole)it.next();
			if(!v.contains(regrole.getRole()))
			//System.out.println("RoleQuery = "+regrole.getRole().getRoleName());
			v.add(regrole.getRole());
		}
		return v;
	}
	public Role QueryPriRole()
	{

		Role role = null;
		Iterator it = r.getReguserroles().iterator();
		while(it.hasNext())
		{
			Reguserrole regrole = (Reguserrole)it.next();
			if(regrole.getPrimaryRole()==1)
			{
				role=regrole.getRole();
			}
		}
		return role;
		
	}
	public Vector GetGuides (Role role)
	{
		Iterator it = role.getAuthorityroles().iterator();
		Vector v = new Vector();
		while(it.hasNext())
		{
			Authorityrole authrole = (Authorityrole)it.next();
			Guide guide = authrole.getAuthority().getSort().getGuide();
			if(!v.contains(guide))
			v.add(guide);
		}
		return v;
	}
	public Vector GetSorts (Role role)
	{
		Iterator it = role.getAuthorityroles().iterator();
		
		Vector v = new Vector();
		while(it.hasNext())
		{
			Authorityrole authrole = (Authorityrole)it.next();
			Sort sort =authrole.getAuthority().getSort();
			if(!v.contains(sort))
			v.add(sort);
		}
		return v;
	}
	public Vector GetGuideSorts (Role role,int GuideID)
	{
		Iterator it = role.getAuthorityroles().iterator();	
		Vector v = new Vector();
		while(it.hasNext())
		{
			Authorityrole authrole = (Authorityrole)it.next();
			Sort sort = authrole.getAuthority().getSort();
			if(!v.contains(sort)&&sort.getGuide().getGuideId()==GuideID)
			{	
				sort.setDefaultAuthId(GetDefaultAuthorityID(role,sort.getSortId()));
				v.add(sort);
			}		
		}
		return v;
	}
	public String GetGuideSortName (Role role,int GuideID)
	{
		Iterator it = role.getAuthorityroles().iterator();
		Vector v = new Vector();
		String sorts="";
		int i=0;
		while(it.hasNext())
		{
			Authorityrole authrole = (Authorityrole)it.next();
			Sort sort = authrole.getAuthority().getSort();
			if(!v.contains(sort)&&sort.getGuide().getGuideId()==GuideID)
			{	
				sort.setDefaultAuthId(GetDefaultAuthorityID(role,sort.getSortId()));
				v.add(sort);

				if(i!=0)
				{
					sorts+="&"+sort.getSortName();
				}
				else
				{
					sorts+=sort.getSortName();
				}
				i++;
			}
			
		}
		return sorts;
	}
	public Vector GetAuthorities(Project project,int sortId)
	{
		Vector v = new Vector();
		if (this.canViewAllPrj())
		{
			Iterator ite_regRole = r.getReguserroles().iterator();
			while (ite_regRole.hasNext())
			{
				Reguserrole regRole = (Reguserrole) ite_regRole.next();
				Iterator it_AutRole = regRole.getRole().getAuthorityroles().iterator();
				while(it_AutRole.hasNext())
				{
					Authorityrole autRole = (Authorityrole)it_AutRole.next();
					if(!v.contains(autRole.getAuthority())&&autRole.getAuthority().getSort().getSortId()==sortId)
					{
						v.add(autRole.getAuthority());
					}
				}
			}
		}
		else
		{
			Iterator ite_ProRegRole = pop.Query("Projectreguserrole", "project.projectId,"+project.getProjectId()+",reguserrole.reguser.regUserId,"+r.getRegUserId(), null, null).iterator();
			while ( ite_ProRegRole.hasNext() )
			{
				Projectreguserrole proRegRole = (Projectreguserrole) ite_ProRegRole.next();
				Iterator it_AutRole = proRegRole.getReguserrole().getRole().getAuthorityroles().iterator();
				while(it_AutRole.hasNext())
				{
					Authorityrole autRole = (Authorityrole)it_AutRole.next();
					if(!v.contains(autRole.getAuthority())&&autRole.getAuthority().getSort().getSortId()==sortId)
					{
						v.add(autRole.getAuthority());
					}
				}
			}
		}
		return v;
	}
	
	public int GetDefaultAuthorityID(Project project,int sortId)
	{
		try
		{
			Vector vec = this.GetAuthorities(project, sortId);
			Authority firstAuthority = (Authority)vec.firstElement();
			return firstAuthority.getAuthorityId();			
		}
		catch(Exception e)
		{
			//System.out.println("defaultAuthorityID:"+e);
			return 0;
		}
	}
	public Vector GetAuthorityName(Role role,int sortId)
	{
		Vector v = new Vector();
		Iterator it = role.getAuthorityroles().iterator();
		while(it.hasNext())
		{
			Authorityrole authrole = (Authorityrole)it.next();
			if(!v.contains(authrole.getAuthority())&&authrole.getAuthority().getSort().getSortId()==sortId)

			{
				v.add(authrole.getAuthority());




			}
		}
		return v;


	}
	public int GetDefaultAuthorityID(Role role,int sortId)
	{
		try
		{
			Vector vec = this.GetAuthorityName(role, sortId);
			Authority firstAuthority = (Authority)vec.firstElement();
			return firstAuthority.getAuthorityId();			
		}
		catch(Exception e)
		{
			//System.out.println("defaultAuthorityID:"+e);
			return 0;
		}
	}
	
	public Vector GetAuthorities(Role role)
	{
		Vector v = new Vector();
		Iterator it = role.getAuthorityroles().iterator();
		while(it.hasNext())
		{
			Authorityrole authrole = (Authorityrole)it.next();
			Authority auth = authrole.getAuthority();
			v.add(auth);
		}
		return v;
	}
	
	public boolean canViewAllPrj()
	{
		Iterator ite_ReguserRoles =r.getReguserroles().iterator();
		while(ite_ReguserRoles.hasNext())
		{	
			Reguserrole rr = (Reguserrole)ite_ReguserRoles.next();
			short canViewAllPrj = rr.getRole().getCanViewAllPrj();
			if (canViewAllPrj==1)
			{
				return true;
			}
			
		}
		return false;
	}
	public Reguser getR() {
		return r;
	}
	public void setR(Reguser r) {
		this.r = r;
	}
		
		
	
}